Skip to content

Command and transaction tidy: one reader core per file, one connection restore - #282

Merged
electricessence merged 2 commits into
masterfrom
refactor/command-transaction-tidy
Dec 2, 2026
Merged

electricessence merged 2 commits into
masterfrom
refactor/command-transaction-tidy

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

What

Two small refinements from the rc.17 pass, and one fix they turned up.

  • One reader core per file. The four DbCommand reader overloads (handler and transform, plain and ValueTask) each carried the same open-and-dispose block, behind #if NETSTANDARD2_0; the SqlCommand overloads did the same behind #if NET472. Each file now has two private cores, one returning ValueTask and one ValueTask<T>, that own the open and the reader's disposal. An overload passes its handler as the argument together with a static lambda that calls it: no closure, so no new allocation. The null checks moved into the cores, which are async, so a null argument is still reported through the returned task, with the same parameter name.
  • One connection restore. The SQL Server package's transaction code carried a copy of the restore pair that differed only in the connection type. It takes a DbConnection, so Core's helper now serves both.
  • Fix. CreateCommand, CreateTextCommand and CreateStoredProcedureCommand on a SqlTransaction with no connection threw ArgumentNullException named connection. They now throw InvalidOperationException("Transaction has no connection."), as Core does. A test and a release-notes bullet cover it.

No public API change. The net line count is about even (+155 / -147): the saving is the four duplicated dispose blocks per file, which the two cores and their comments take back.

Checks

  • Allocation per call of the four DbCommand overloads, measured on this branch and on master with the same probe (bytes per call, handler completing synchronously): 112 on both, for every overload. With a handler that suspends, 407 against 403 and 439 against 436: a difference of 3 to 4 bytes a call, which is not a whole state-machine box (a multiple of 8), so it is measurement noise from Task.Yield. The probe was scratch and is not in the PR; no existing allocation test covers these overloads.
  • Core, MSSqlClient and the tests build with no errors or warnings, every target framework (VS MSBuild, Release).
  • Core test suite on net11.0: 15,573 passed, 0 failed.

Review

Three read-only reviewers looked at the diff: correctness and allocation, minimalism and readability, and tests and docs.

  • Correctness and allocation: no finding. Argument exceptions still arrive through the returned task with the same parameter names, in the same order; the reader is disposed on every path, including a throwing handler, in both #if branches; the two cores do not make overload resolution ambiguous; the lambdas capture nothing.
  • Kept and fixed: a doc comment on a private core that nothing generates docs for was removed; the two files' comment blocks now say the same thing and say why there are two cores; a test comment overstated what is known about an ended transaction.
  • Not kept: a note on the behavior change for callers who caught ArgumentNullException: the release-notes bullet already says what it threw before and what it throws now.
  • Not in this PR: the unpublished SqlClient project keeps its own restore helpers.

🤖 Generated with Claude Code

electricessence and others added 2 commits December 1, 2026 12:44
…ction restore

The four DbCommand reader overloads (handler and transform, plain and
ValueTask) each opened the connection and disposed the reader with the same
four-line block, and the SqlCommand overloads did the same. Each file now has
two private cores, one returning ValueTask and one ValueTask<T>, that own the
open and the disposal. An overload passes its handler as the argument together
with a static lambda that calls it, so there is no closure and the per-call
allocation is unchanged. The null checks moved into the cores, which are async,
so a null argument is still reported through the returned task.

The SQL Server package's transaction code no longer carries its own copy of the
restore pair. It takes a DbConnection, so the core package's helper serves
both.

Fix: CreateCommand, CreateTextCommand and CreateStoredProcedureCommand on a
SqlTransaction that has no connection threw ArgumentNullException named
"connection". They now throw InvalidOperationException("Transaction has no
connection."), as the core package does. A test and a release note cover it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the explicit private on the two cores, as the rest of the code does,
and wrap their comment to the soft line limit. No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit 5e0d3ea into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the refactor/command-transaction-tidy branch December 2, 2026 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant